"""
接口：trade_cal，可以通过数据工具调试和查看数据。
描述：获取各大交易所交易日历数据,默认提取的是上交所
积分：需2000积分
"""

import pandas as pd
import tushare as ts
from sqlalchemy import text
from app_config import get_engine

engine = get_engine()


def read_data():
    sql = """SELECT * FROM trade_cal"""
    data_frame = pd.read_sql_query(sql, engine)
    return data_frame


def rewrite_data(data_frame):
    sql = """TRUNCATE TABLE trade_cal"""
    with engine.connect() as conn:
        conn.execute(text(sql))
    res = data_frame.to_sql('trade_cal', engine, index=False, if_exists='append', chunksize=5000)
    print(res)


def get_data():
    pro = ts.pro_api()
    data = pro.trade_cal(exchange='SSE', is_open='1', start_date='20221101', end_date='20231031', fields='cal_date')
    return data


if __name__ == '__main__':
    df = get_data()
    rewrite_data(df)
    print(df)
